<%@ page language="java" contentType="text/html; charset=utf-8"	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib tagdir="/WEB-INF/tags/sys" prefix="sys"%>
<%@ taglib uri="/WEB-INF/tlds/webui.tld" prefix="webui"%>

<webui:enum var="roleEnum" code="RoleEnum" />
<webui:enum var="orgEnum" code="OrgEnum" />

<webui:panel title="操作员信息" id="operatorEditor" style="display:none">
	<webui:form method="get" styleClass="form-horizontal" id="operatorEditorForm">
		<input type="hidden" value="" name="id" id="editFormOperatorId" />
		<webui:input label="账号" name="loginName" id="editFormOperatorLoginName" labelClass="col-sm-2" inputClass="col-sm-10" 
		placeholder="请输入至少4个字符" type="text" split="true">
			<webui:validator required="true" minlength="4"/>
		</webui:input>
		<webui:input label="密码" name="password" id="editFormOperatorPassword" labelClass="col-sm-2" inputClass="col-sm-10" 
		placeholder="请输入至少4字符" type="password" split="true">
			<webui:validator required="true" minlength="4"/>
		</webui:input>
		<webui:input label="姓名" name="name" id="editFormOperatorName" labelClass="col-sm-2" inputClass="col-sm-10" 
		placeholder="请输入2-8个中文字符" type="text" split="true">
			<webui:validator required="true" minlength="2" maxlength="8"/>
		</webui:input>
		<webui:input label="邮箱" name="email" id="editFormOperatorEmail" labelClass="col-sm-2" inputClass="col-sm-10" 
		placeholder="请输入邮箱地址" type="text" split="true">
			<webui:validator email="true" />
		</webui:input>
		<webui:input label="手机" name="mobile" id="editFormOperatorMobile" labelClass="col-sm-2" inputClass="col-sm-10" type="text" split="true">
			<webui:validator digits="true" minlength="11" maxlength="11">{'minlength':'手机号码格式不正确','maxlength':'手机号码格式不正确'}</webui:validator>
		</webui:input>
		<webui:select label="角色" name="roles" id="editFormOperatorRoles"
			selectClass="chosen-select" labelClass="col-sm-2" inputClass="col-sm-10"
			lookup="${roleEnum}" multiple="true" split="true">
			<webui:validator selected="true" />
		</webui:select>
		<webui:select label="组织" name="orgId" id="editFormOperatorOrgId" 
			labelClass="col-sm-2" inputClass="col-sm-10"
			lookup="${orgEnum}" split="true">
			<webui:validator selected="true" />
		</webui:select>
	</webui:form>
	<div class="row">
		<div class="col-md-8 " style="float: right">
			<webui:button type="button" buttonClass="btn-white" onclick="cancelOperator()">取消</webui:button>
			<webui:button id="operatorSaveBtn" type="button" buttonClass="btn-primary" ladda="true" onclick="saveOperator()">保存</webui:button>
		</div>
	</div>
</webui:panel>

<script>
	function createOperator(){
		$("#editFormOperatorLoginName").attr("disabled",false)
		$("#editFormOperatorPassword_div").show();
		$("#editFormOperatorPassword_split").show();
		grid_operator.create()
	}
	function resetValidateMsg(){
	    $("#editFormOperatorPassword").removeAttr("readonly");
		operatorEditorFormValidator.resetForm();
	}

	function editOperator(id){
		$("#editFormOperatorPassword_div").hide();
		$("#editFormOperatorPassword_split").hide();
		$.ajax({
 	 	    type:"GET",
 	 	    async:false,
 	 	    url:"${contextPath}/rest/operators/"+id,
 	 	    success:function(data){
 	 			grid_operator.create();
 	 	    	$("#editFormOperatorId").val(data.id);
 	 	    	$("#editFormOperatorLoginName").val(data.loginName);
 	 	    	$("#editFormOperatorPassword").val(data.password);
 	 	    	$("#editFormOperatorPassword").attr("readonly","readonly");
 	 	    	$("#editFormOperatorName").val(data.name);
 	 	    	$("#editFormOperatorEmail").val(data.email);
 	 	    	$("#editFormOperatorMobile").val(data.mobile);
 	 	    	$("#editFormOperatorRoles").val(data.roleIds);
 	 	    	$("#editFormOperatorRoles").trigger("chosen:updated");
 	 	    	$("#editFormOperatorOrgId").val(data.orgId);
				$("#editFormOperatorLoginName").attr("disabled",true)
	 	    }
 	 	});
	}

	function cancelOperator(){
		operatorSaveBtn.ladda('stop');
		grid_operator.show();
	}

	function saveOperator(){	
		if(operatorEditorFormValidator.form()){
			operatorSaveBtn.ladda('start');
			$.ajax({
				method:"post",
				url:'${contextPath}/sys/operator/save.action',
				dataType:"json",
				contentType:"application/json; charset=utf-8",
				data:$('form#operatorEditorForm').serializeJSON(),
				success:function(){
					operatorSaveBtn.ladda('stop');
					showMessage('提示',
						($("#editFormOperatorId").val()=="")?'保存成功。':'修改成功。',
						function(){
							$('#operatorEditorForm').reset();
							grid_operator.refresh();
						}
					);
				}
			});
		}			
	}
</script>